// 根据屏幕比例自动匹配
function SmatchScreen (ele, mode, designWidth, designHeight, isXScale, isYScale, container) {
  mode = mode || 'v-v'
  designWidth = designWidth || 750
  designHeight = designHeight || 1200
  isXScale = isXScale || false
  isYScale = isYScale || false

  var winW = (container && container.clientWidth) || (window.innerWidth)
  var winH = (container && container.clientHeight) || (window.innerHeight)
  // 窗口中心坐标
  var winCenterX = winW / 2
  var winCenterY = winH / 2
  // 设计稿中心坐标
  var designCenterX = designWidth / 2
  var designCenterY = designHeight / 2

  // 中心距离
  var xDis = designCenterX - winCenterX
  var yDis = designCenterY - winCenterY
  var ratio, sRatio

  if (mode === 'v-v') {
    // 设计稿缩放到窗口高度
    ratio = winH / designHeight
    // 中心对其
    setDesignScaleAndMove(ele, ratio, ratio, -xDis, -yDis, 0)
    // 如果需要水平百分百显示，则再次缩放水平尺寸
    if (isXScale) { // 设置手机x方向全屏
      sRatio = winW / designWidth
      setDesignScaleAndMove(ele, sRatio, ratio, -xDis, -yDis, 0)
    }
  } else if (mode === 'h-h') {
    ratio = winH / designWidth
    setDesignScaleAndMove(ele, ratio, ratio, -xDis, -yDis, 90)
    if (isXScale) {
      sRatio = winW / designHeight
      setDesignScaleAndMove(ele, sRatio, ratio, -xDis, -yDis, 90) // scale旋转之前的Y方向
    }
  } else if (mode === 'h-v') {
    ratio = winW / designWidth
    setDesignScaleAndMove(ele, ratio, ratio, -xDis, -yDis, 0)
    if (isYScale) {
      sRatio = winH / designHeight
      setDesignScaleAndMove(ele, ratio, sRatio, -xDis, -yDis, 0)
    }
  } else if (mode === 'v-h') {
    ratio = winW / designHeight
    setDesignScaleAndMove(ele, ratio, ratio, -xDis, -yDis, 90)
    if (isYScale) {
      sRatio = winH / designWidth
      setDesignScaleAndMove(ele, ratio, sRatio, -xDis, -yDis, 90)
    }
  } else if (mode === 'w-t') {
    // width-bottom 宽度100% top 0
    ratio = winW / designWidth
    setDesignScaleAndMove(ele, ratio, ratio, 0, 0, 0, 'left', 'top')
    // 消除scale translate后元素在父容器的占位,否则长图下方会有留白
    ele.parentNode.style.height = ele.offsetHeight * ratio + 'px'
    ele.parentNode.style.width = ele.offsetWidth * ratio + 'px'
  } else if (mode === 'w-b') {
    // width-bottom 宽度100% 高等比缩放 bottom 0
    ratio = winW / designWidth
    var offsetH = designHeight - winH
    setDesignScaleAndMove(ele, ratio, ratio, 0, -offsetH, 0, 'left', 'bottom')
  } else if (mode === 'h-l') {
    // height-left 高度是容器的100% 宽度等比缩放 位置靠左侧
    ratio = winH / designHeight
    setDesignScaleAndMove(ele, ratio, ratio, 0, 0, 0, 'left', 'top')
  } else if (mode === 'h-r') {
    // height-right   高度是容器的100% 宽度等比缩放 位置靠右侧
    ratio = winH / designHeight
    var offsetX = winW - designWidth
    setDesignScaleAndMove(ele, ratio, ratio, offsetX, 0, 0, 'right', 'top')
  }

  function setDesignScaleAndMove (ele, ratioX, ratioY, disX, disY, angle, h, v) {
    h = h || 'center'
    v = v || 'center'
    var prefixes = ['moz', 'ms', 'o', 'webkit', '']
    prefixes.forEach(function (item, index, arr) {
      ele.style[item + 'transformOrigin'] = h + ' ' + v
      ele.style[item + 'Transform'] = 'translate(' + disX + 'px,' + disY + 'px) ' + 'scale(' + ratioX + ',' + ratioY + ') ' +
          'rotate(' + angle + 'deg)'
    })
  }
};
